<?php
/**
 * Description of excel
 *
 * @author Yefng
 */

@set_time_limit(0);
include_once PLUG_DIR . 'PHPExcel.php';
class excel
{
    var $row = 1;
	
	/**
	 * 数值列
	 * @var type 
	 */
    var $cellValue = array();

	/**
	 * 
	 * @param type $res[0]
	 */
    function fieldToHead($arr)
    {
        $arr = $arr ? array_keys($arr) : array();
        foreach ($arr as $column=>$field)
            $this->objSheet->setCellValueByColumnAndRow($column, $this->row, lang::g( 'column_'.$field));
        $this->row++;
		return $this;
    }
	function createBody($res)
    {
		foreach ($res as $arr)
		 {
			$column = 0;
            foreach ($arr as $field=>$val)
             {
                if (isset($this->cellValue[$field]))
                   $val = $this->cellValue[$field][$val];
                $this->objSheet->setCellValueByColumnAndRow($column++, $this->row, $val);
             }
            $this->row++;    
         }
		return $this;
    }
	
	// 定义文件
	function open()
	{
		$this->Excel = new PHPExcel();
		$this->objSheet = $this->Excel->setActiveSheetIndex(0);
		return $this;
	}
	function save(){}
	/**
     * 文件输出
     * @param type $filename 文件名
     */
    function out($filename=null)
    {
		$filename or $filename= date('Ymd');
        header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'. $filename  .'.xls"');
		header('Cache-Control: max-age=0');
		$objWriter = PHPExcel_IOFactory::createWriter($this->Excel, 'Excel5');
		$objWriter->save('php://output');
		die; 
    }
}
